var HostSpy = {
	navigateURL: "http://hostspy.org/%HOST%",
	currentHostName : null,
	
	onLoad: function() 
	{
		document.getElementById("contentAreaContextMenu").addEventListener("popupshowing",HostSpy.onContextMenu,false);
	},
	onContextMenu: function()
	{
		HostSpy.getHostName();
	
		document.getElementById("hostspyMenuItem").label = "Host Spy \""+ HostSpy.currentHostName +"\"";
		
		var isMenuItemHidden = (HostSpy.currentHostName == null);
		document.getElementById("hostspyMenuItem").hidden = isMenuItemHidden;
		document.getElementById("hostspySeperator").hidden = isMenuItemHidden;
	},	
	getHostName: function()
	{
		var url = getBrowser().contentDocument.location.href;
		if(url.indexOf("://") > 0)
		{
			url = url.substring(url.indexOf('://')+3);  // remove protocol type.
			url = url.substring(0, url.indexOf('/'));  // remove execution file path and querystring.
			
			if(url.indexOf('@') > 0)
				url = url.substring(url.indexOf('@') + 1); // remove login information (user:pass@example.com)
			if(url.indexOf(':') > 0)
				url = url.substring(0, url.indexOf(':'));  // remove port (example.com:81)
				
			url = (url == "") ? null : url;  // convert empty to null.
				
			HostSpy.currentHostName = url;
		}
		else
		{
			HostSpy.currentHostName = null;
		}
	},
	navigate: function()
	{
		var url = HostSpy.navigateURL.replace("%HOST%", HostSpy.currentHostName);
		getBrowser().selectedTab = getBrowser().addTab(url);
	}
}

window.addEventListener("load", function(e) { HostSpy.onLoad(e); }, true);